package com.wlcorp.forcazamis.dao.storageassign.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.storageassign.IStorageAssignSampleDAO;
import com.wlcorp.forcazamis.domain.storageassign.StorageAssignSample;

public class StorageAssignSampleDAOImpl extends HibernateDaoSupport implements
		IStorageAssignSampleDAO {

	@SuppressWarnings("unchecked")
	public List getStorageAssignSampleList(final StorageAssignSample storageAssignSample)
			throws DaoException {
		final StringBuffer queryString = new StringBuffer("select storageAssignSample from StorageAssignSample as storageAssignSample where 1=1 and storageAssignSample.deleteFlag=false");
		if(null != storageAssignSample){
			if(null != storageAssignSample.getStorageAssignId()){
				queryString.append(" and storageAssignSample.storageAssign.storageAssignId = ").append(storageAssignSample.getStorageAssignId());
			}
		}
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != storageAssignSample && null != storageAssignSample.getMinRownum()){
					query.setFirstResult(storageAssignSample.getMinRownum());
					query.setMaxResults(storageAssignSample.getNumPerPage());
				}
				return query.list();
			}
			
		});
	}

	public Long getStorageAssignSampleCount(StorageAssignSample storageAssignSample)
			throws DaoException {
		final StringBuffer queryString = new StringBuffer("select count(storageAssignSample) from StorageAssignSample as storageAssignSample where 1=1 and storageAssignSample.deleteFlag=false");
		if(null != storageAssignSample){
			if(null != storageAssignSample.getStorageAssignId()){
				queryString.append(" and storageAssignSample.storageAssign.storageAssignId = ").append(storageAssignSample.getStorageAssignId());
			}
		}
		return (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				return query.uniqueResult();
			}
		});
	}

	public StorageAssignSample getStorageAssignSampleById(Long storageAssignSampleId)
			throws DaoException {
		return (StorageAssignSample)getHibernateTemplate().get(StorageAssignSample.class,storageAssignSampleId);
	}

	public void deleteStorageAssignSample(StorageAssignSample storageAssignSample)
			throws DaoException {
		storageAssignSample.setDeleteFlag(true);
		getHibernateTemplate().update(storageAssignSample);
	}

	public void saveStorageAssignSample(StorageAssignSample storageAssignSample)
			throws DaoException {
		getHibernateTemplate().saveOrUpdate(storageAssignSample);
		getHibernateTemplate().flush();
		getHibernateTemplate().clear();
	}

}
